import 'dart:math' as math;

import 'package:flutter/material.dart';

import '../base/christmas_light_style.dart';

//ignore_for_file: prefer_const_constructors
final _style = ButtonStyle(
  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  elevation: MaterialStateProperty.all(.0),
  backgroundColor: MaterialStateProperty.all(Colors.transparent),
  shape: MaterialStateProperty.all(
    const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(8)),
    ),
  ),
);

/// 渐变按钮
class ChristmasLightPositiveButton extends StatelessWidget {
  final List<Color> _gradientColor = [];
  final Widget child;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress;

  ChristmasLightPositiveButton({
    Key? key,
    required this.child,
    this.onPressed,
    this.onLongPress,
  }) : super(key: key);

  bool get enable => onPressed != null || onLongPress != null;

  @override
  Widget build(BuildContext context) {
    if (_gradientColor.isEmpty) {
      // final app = AppSymbolWidget.maybeOf(context)?.app;
      // todo 判断不同app使用不同样式
      _gradientColor.addAll([
        christmasLightStyle.gradientColors.first.withAlpha(
          enable ? 0xFF : 0xC8,
        ),
        christmasLightStyle.gradientColors[1].withAlpha(enable ? 0xFF : 0xC8),
      ]);
    }
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: _gradientColor),
        borderRadius: const BorderRadius.all(Radius.circular(8)),
      ),
      child: ElevatedButton(
        onPressed: onPressed,
        onLongPress: onLongPress,
        style: _style,
        child: child,
      ),
    );
  }
}

/// 背景色不明显按钮
class ChristmasLightNegativeButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress;

  const ChristmasLightNegativeButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.onLongPress,
  }) : super(key: key);

  bool get enable => onPressed != null || onLongPress != null;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.all(Radius.circular(8)),
        color: christmasLightStyle.positiveButtonColor.withAlpha(
          enable ? 0xFF : 0xC8,
        ),
      ),
      child: ElevatedButton(
        onPressed: onPressed,
        onLongPress: onLongPress,
        style: _style,
        child: Text(text),
      ),
    );
  }
}

/// 边框按钮
class ChristmasLightBorderButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress;

  const ChristmasLightBorderButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.onLongPress,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onPressed,
      onLongPress: onLongPress,
      child: Text(text),
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.transparent),
        elevation: MaterialStateProperty.all(0),
        shape: MaterialStateProperty.all(
          StadiumBorder(side: BorderSide(color: Colors.white, width: 1)),
        ),
      ),
    );
  }
}

/// 颜色组合按钮
class ChristmasLightColorCombinationButton extends StatelessWidget {
  final List<Color> colors;
  final VoidCallback? onPressed;
  final GestureLongPressCallback? onLongPress;
  final bool isSelected;

  const ChristmasLightColorCombinationButton({
    super.key,
    required this.colors,
    this.onPressed,
  this.onLongPress,
    this.isSelected = false
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        bool _isW = constraints.maxWidth.isInfinite;
        bool _isH = constraints.maxHeight.isInfinite;
        double _width = _isW ? 50 : constraints.maxWidth;
        double _height = _isH ? 50 : constraints.maxHeight;
        return GestureDetector(
          onTap: onPressed,
          onLongPress: onLongPress,
          child: Container(
            width: _width,
            height: _height,
            padding: const EdgeInsets.all(1.0),
            decoration: BoxDecoration(
              border: Border.all(color: isSelected?Colors.white:Colors.white.withOpacity(0.3), width: 2),
            ),
            child: ClipRect(
              child: CustomPaint(
                painter: MyCustomPainter(colors: colors),
              ),
            ),
          ),
        );
      },
    );
  }
}

class MyCustomPainter extends CustomPainter {
  final List<Color> colors;

  MyCustomPainter({required this.colors});

  @override
  void paint(Canvas canvas, Size size) {
    _canvas(canvas, size);
  }

  void _canvas(Canvas canvas, Size size) {
    // 倒置顺序
    List<Color> _colors = [];
    for (int i = colors.length - 1; i >= 0; i--) {
      _colors.add(colors[i]);
    }

    // 画布中心点
    Offset center = Offset(size.width / 2, size.height / 2);
    // A² + B²
    num value = math.pow(size.width, 2) + math.pow(size.height, 2);
    // √C
    double width = math.sqrt(value);
    double height = width / colors.length;

    // 矩形中心点
    Offset rectCenter = Offset(width / 2, width / 2);
    //把画出的矩形移动至画布中心
    double vx = rectCenter.dx - center.dx;
    double vy = rectCenter.dy - center.dy;
    canvas.translate(-vx, -vy);

    canvas.save();

    // 旋转角度
    canvas.translate(center.dx + vx, center.dy + vy);
    canvas.rotate((math.pi / 4) * 3);
    canvas.translate(-(center.dx + vx), -(center.dy + vy));

    Offset origin = Offset.zero;
    Paint paint = Paint()..color = Colors.white;
    for (Color color in _colors) {
      paint.color = color;
      Rect rect = Rect.fromLTWH(origin.dx, origin.dy, width, height);
      canvas.drawRect(rect, paint);
      double dy = origin.dy + height;
      origin = Offset(0.0, dy);
    }
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
